package com.aijk.commons.lock.perf;


import com.aijk.commons.lock.Lock;
import com.aijk.commons.lock.RedisServer;
import com.aijk.commons.lock.impl.RedisLock;
import com.aijk.perf.Perf;
import redis.clients.jedis.JedisCluster;

/**
 * Redis 阻塞锁QPS测试
 */
public class RedisLockPerf {

    /**
     * 锁名称
     */
    private static final String LOCK_KEY = "lock.lock";
    /**
     * 锁超时时间
     */
    private static final long LOCK_EXPIRE = 5 * 1000;
    /**
     * 循环次数
     */
    private static final int loopCount = 100000;
    /**
     * 线程数
     */
    private static final int threadCount = 10;
    /**
     * 打印QPS间隔
     */
    private static final int logInterval = 1000;

    public static void main(String[] args) throws Exception {

        JedisCluster cluster = RedisServer.getConfigCluster();

        Perf perf = new Perf() {

            @Override
            public TaskInThread buildTaskInThread() {
                return new TaskInThread() {

                    @Override
                    public void initTask() throws Exception {

                    }

                    @Override
                    public void doTask() throws Exception {
                        Lock lock = new RedisLock(cluster, LOCK_KEY + System.currentTimeMillis(), LOCK_EXPIRE);
                        lock.lock();
                        try {

                        } finally {
                            lock.unlock();
                        }
                    }
                };
            }

        };
        perf.loopCount = loopCount;
        perf.threadCount = threadCount;
        perf.logInterval = logInterval;
        perf.run();
        perf.close();

    }
}
